package niumao.minecraft.uhc.listener;

import niumao.minecraft.uhc.Main;
import niumao.minecraft.uhc.Util;
import org.bukkit.ChatColor;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.HashMap;
import java.util.Objects;
import java.util.UUID;

public class Perun_Exc implements Listener {
    private static final Main plugin = Main.getPlugin(Main.class);

    public static HashMap<UUID, Integer> Peruncooldown = new HashMap<>();
    public static HashMap<UUID, Integer> Exccooldown = new HashMap<>();

    public static int peruncooldowntime = 8;
    public static int exccooldowntime = 5;

    public static boolean perunflag = false;
    public static boolean exaflag = false;

    @EventHandler
    public void on(EntityDamageByEntityEvent event) {
        //不是玩家造成的伤害则返回
        if (!(event.getDamager() instanceof Player)) return;
        Player player = (Player) event.getDamager();
        Entity entity = event.getEntity();
        UUID uuid = player.getUniqueId();
        ItemMeta itemMeta = player.getInventory().getItemInMainHand().getItemMeta();
        if (itemMeta == null) return;

        if (itemMeta.getDisplayName().equals(Util.getStr("Perun"))) {
            if (!Peruncooldown.containsKey(uuid) || Peruncooldown.get(player.getUniqueId()) == 0) {
                entity.getWorld().strikeLightning(entity.getLocation());
                Peruncooldown.put(uuid, peruncooldowntime);
                perunflag = true;
            }
        } else if (itemMeta.getDisplayName().equals(Util.getStr("Excalibur"))) {
            if (!Exccooldown.containsKey(uuid) || Exccooldown.get(player.getUniqueId()) == 0) {
                entity.getWorld().createExplosion(entity.getLocation().add(0, 0.7, 0), 0.8f);
                Exccooldown.put(uuid, exccooldowntime);
                exaflag = true;
            }
        }
    }

    public static void runnablerunner() {
        new BukkitRunnable() {
            @Override
            public void run() {
                for (UUID uuid : Peruncooldown.keySet()) {
                    Player player = Main.getInstance().getServer().getPlayer(uuid);
                    if (player == null) return;
                    int time = Peruncooldown.get(uuid);
                    if (time-- <= 1) {
                        if (perunflag) {
                            player.sendMessage(Objects.requireNonNull(plugin.getConfig().getString("item." + Main.getLang() + ".Perun.message")));
                            perunflag = false;
                        }
                    }
                    if (time < 0) time = 0;
                    Peruncooldown.put(uuid, time);
                }
                for (UUID uuid : Exccooldown.keySet()) {
                    Player player = Main.getInstance().getServer().getPlayer(uuid);
                    if (player == null) return;
                    int time = Exccooldown.get(uuid);
                    if (time-- <= 1) {
                        if (exaflag) {
                            player.sendMessage(Objects.requireNonNull(plugin.getConfig().getString("item." + Main.getLang() + ".Excalibur.message")));
                            exaflag = false;
                        }
                    }
                    if (time < 0) time = 0;
                    Exccooldown.put(uuid, time);
                }
            }

        }.runTaskTimer(Main.getInstance(), 0L, 20L);
    }

}
